package com.gd.deron.smartrobot.baobao;


import com.gd.deron.smartrobot.baobao.utils.BlueTools;
import com.gd.deron.smartrobot.baobao.utils.Config;
import com.gd.deron.smartrobot.baobao.utils.InstructionUtil;
import com.gd.deron.smartrobot.baobao.utils.ToastUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class ManualControlActivity extends BaseActivity implements  OnTouchListener {
   

    private static final String TAG = "ManualControlActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_control);

        initView();
    }

    private void initView() {
    	
    	findViewById(R.id.turn_left).setOnTouchListener(this);
		findViewById(R.id.turn_right).setOnTouchListener(this);
		findViewById(R.id.move_forward).setOnTouchListener(this);
		findViewById(R.id.move_backward).setOnTouchListener(this);
		
	}

	@Override
    protected void onStart() {
		this.registerReceiver(receiver, Config.getfilter());
    	super.onStart();
    }
    @Override
    protected void onResume() {

        super.onResume();
    }
	BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context contextR, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Config.CON_SUCCESS)) {
				mHandler.sendEmptyMessage(0x11);
			} else if (action.equals(Config.DISCONNECTED)) {
				mHandler.sendEmptyMessage(0x12);
			} else if (action.equals(Config.FIND_DEVICE)) {// 发现设备
				mHandler.sendEmptyMessage(0x13);
			}
		}
	};
	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0x11:// 成功
				
				break;
			case 0x12:// 断开
				ManualControlActivity.this.finish();
				ToastUtils.show(ManualControlActivity.this, "连接已断开！");
				break;
			case 0x13:// 发现设备
				
				break;
			default:
				break;
			}
		}
	};
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				switch(v.getId()) {
				case R.id.turn_left://左转
						BlueTools.sendData(InstructionUtil.AIJIA_SITU_TURN_LEFT);
					break;
				case R.id.turn_right://右转
						BlueTools.sendData(InstructionUtil.AIJIA_SITU_TURN_RIGHT);
					break;
				case R.id.move_forward://
						BlueTools.sendData(InstructionUtil.AIJIA_FORWARD);
					break;
				case R.id.move_backward://
						BlueTools.sendData(InstructionUtil.AIJIA_BACK);
					break;
				}
				break;
			case MotionEvent.ACTION_UP:
				switch(v.getId()) {
				case R.id.turn_left://左转
				case R.id.turn_right://右转
				case R.id.move_forward://
				case R.id.move_backward://
					BlueTools.sendData(InstructionUtil.AIJIA_STOP);
					break;
				}
		}
		return true;
	}
   

    @Override
    protected void onDestroy() {
        super.onDestroy();
      unregisterReceiver(receiver);
        
    }



}